package cn.weiwh.mailsend.mail.manager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by 魏望浩 on 2018/1/24.
 * JavaMail发送
 **/
//@Component
public class MailManager {
    private static final Logger logger= LoggerFactory.getLogger(MailManager.class);
//    private static final String protocol="SMTP";
//    private static final String host = "smtp.163.com";  //邮件服务器//smtp.gd.chinamobile.com//mail.gd.chinamobile.com
//    private static final String port = "25";
//    private static final String userName = "15322888321@163.com";                                     //gywgfxs4@gd.chinamobile.com
//    private static final String pwd = "wwh123456";  //客户端授权码（这里用客户端授权码登陆，而不是用密码登陆）//Gmcc@1234
//    private static final String timeout = "1000";//超时时间
//    private static final String from = "15322888321@163.com";//gywgfxs4@gd.chinamobile.com
//    //private static final String to = "1162868738@qq.com";//chenjiehan@gd.chinamobile.com
//    private static final String title="测试自动发送邮件"; //邮件发送title



    //发送邮件
    public static void sendMail(String emailMsg)
              {
        String to[] = { "2668173317@qq.com", "1162868738@qq.com" };// 发送到那里
        // 1.创建一个程序与邮件服务器会话对象 Session
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "SMTP");
        props.setProperty("mail.smtp.host", "smtp.163.com");
        props.setProperty("mail.smtp.port", "25");
        // 指定验证为true
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.timeout","5000");
        // 验证账号及密码，密码需要是第三方授权码
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("15953185712@163.com", "czxsqm521");
            }
        };
        Session session = Session.getInstance(props, auth);
        try{
            // 2.创建一个Message，它相当于是邮件内容
            Message message = new MimeMessage(session);
            // 设置发送者
            message.setFrom(new InternetAddress("15953185712@163.com"));
            // 设置发送方式与接收者
            InternetAddress[] sendTo = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++) {
                logger.info("wwh--发送到:" + to[i]);
                sendTo[i] = new InternetAddress(to[i]);
            }
            message.addRecipients(Message.RecipientType.TO, sendTo);
           // message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(email));
            // 设置主题
            message.setSubject("邮件发送测试wwh1");

            // 设置内容
            message.setContent(emailMsg, "text/html;charset=utf-8");

            // 3.创建 Transport用于将邮件发送
            Transport.send(message);
            logger.info("wwh:发送完毕！");
        }catch (MessagingException e){
            logger.info("wwh:发送失败！");
            e.printStackTrace();
        }


    }

}
